package com.ruoyi.cloudpc.service.impl;

import com.ruoyi.cloudpc.domain.CloudPromoterApprove;
import com.ruoyi.cloudpc.mapper.CloudPromoterApproveMapper;
import com.ruoyi.cloudpc.service.ICloudPromoterApproveService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 申请推广人Service业务层处理
 *
 * @author Ten
 * @date 2025-10-09
 */
@Slf4j
@Service
public class CloudPromoterApproveServiceImpl implements ICloudPromoterApproveService {
    @Autowired
    private CloudPromoterApproveMapper cloudPromoterApproveMapper;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询申请推广人
     *
     * @param userId 申请推广人主键
     * @return 申请推广人
     */
    @Override
    public CloudPromoterApprove selectCloudPromoterApproveByUserId(Long userId) {
        return cloudPromoterApproveMapper.selectCloudPromoterApproveByUserId(userId);
    }

    /**
     * 查询申请推广人列表
     *
     * @param cloudPromoterApprove 申请推广人
     * @return 申请推广人
     */
    @Override
    public List<CloudPromoterApprove> selectCloudPromoterApproveList(CloudPromoterApprove cloudPromoterApprove) {
        return cloudPromoterApproveMapper.selectCloudPromoterApproveList(cloudPromoterApprove);
    }

    /**
     * 新增申请推广人
     *
     * @param cloudPromoterApprove 申请推广人
     * @return 结果
     */
    @Override
    public int insertCloudPromoterApprove(CloudPromoterApprove cloudPromoterApprove) {
        // 判断当前用户是否是推广人
        Long currentUserId = SecurityUtils.getUserId();
        SysUser currentUser = sysUserService.selectUserById(currentUserId);
        if ("1".equals(currentUser.getIsPromotion())) {
            throw new RuntimeException("用户：" + currentUser.getUserName() + "已是推广人，无需再次申请");
        }

        CloudPromoterApprove cloudPromoterApprove1 = selectCloudPromoterApproveByUserId(currentUserId);
        if (cloudPromoterApprove1 != null) {
            throw new RuntimeException("用户：" + currentUser.getUserName() + "已有申请记录，无需再次申请");
        }

        cloudPromoterApprove.setUserId(currentUserId);
        cloudPromoterApprove.setCreateByAndTime();
        return cloudPromoterApproveMapper.insertCloudPromoterApprove(cloudPromoterApprove);
    }

    /**
     * 修改申请推广人
     *
     * @param cloudPromoterApprove 申请推广人
     * @return 结果
     */
    @Override
    public int updateCloudPromoterApprove(CloudPromoterApprove cloudPromoterApprove) {
        cloudPromoterApprove.setUpdateByAndTime();
        return cloudPromoterApproveMapper.updateCloudPromoterApprove(cloudPromoterApprove);
    }

    /**
     * 审批
     *
     * @param cloudPromoterApprove 申请推广人
     * @return 结果
     */
    @Transactional
    @Override
    public int approve(CloudPromoterApprove cloudPromoterApprove) {
        cloudPromoterApprove.setUpdateByAndTime();
        int result = cloudPromoterApproveMapper.updateCloudPromoterApprove(cloudPromoterApprove);
        if (result != 0 && "Y".equals(cloudPromoterApprove.getApproveFlag())) {
//            SysUser sysUser = sysUserService.selectUserById(cloudPromoterApprove.getUserId());
            SysUser sysUser = new SysUser();
            sysUser.setUserId(cloudPromoterApprove.getUserId());
            sysUser.setIsPromotion("1");
            if (sysUserService.updateUserIsPromotion(sysUser) == 0) {
                log.error("修改用户为推广人失败");
                throw new RuntimeException("系统异常，请联系管理员");
            }
        }
        return result;
    }

    /**
     * 批量删除申请推广人
     *
     * @param userIds 需要删除的申请推广人主键
     * @return 结果
     */
    @Override
    public int deleteCloudPromoterApproveByUserIds(Long[] userIds) {
        return cloudPromoterApproveMapper.deleteCloudPromoterApproveByUserIds(userIds);
    }

    /**
     * 删除申请推广人信息
     *
     * @param userId 申请推广人主键
     * @return 结果
     */
    @Override
    public int deleteCloudPromoterApproveByUserId(Long userId) {
        return cloudPromoterApproveMapper.deleteCloudPromoterApproveByUserId(userId);
    }
}
